package com.linln.admin.home;

import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import com.linln.modules.homerotation.domain.HomeRotation;
import com.linln.modules.homerotation.service.HomeRotationService;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.admin.home
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.admin.home
 * @Description: TODO
 * @date Date : 2021年01月11日 16:29
 */
@RequestMapping("carousel")
@Controller
public class CarouselController {

    @Autowired
    private HomeRotationService homeRotationService;

    @GetMapping("index")
    public String index() {
        return "/home/carousel/index";
    }

    @PostMapping
    @ResponseBody
    public JsonResult addHoneRotation(@RequestParam("file") MultipartFile file, HomeRotation homeRotation) {
        List<HomeRotation> honeRotationList = homeRotationService.getHoneRotation(0);
        if(honeRotationList.size()>=5){
           return JsonResult.error("数量不能大于5张");
        }
        homeRotation.setHrType(0);
        return homeRotationService.addHoneRotation(file, homeRotation) ? JsonResult.ok() : JsonResult.error();
    }
    @PutMapping
    @ResponseBody
    public JsonResult updateHoneRotation(@RequestParam("file") MultipartFile file, HomeRotation homeRotation) {
        homeRotation.setHrType(0);
        return homeRotationService.updateHoneRotation(file, homeRotation) ? JsonResult.ok() : JsonResult.error();
    }
    @GetMapping
    @ResponseBody
    public PageResult getHoneRotation() {
        return new PageResult(homeRotationService.getHoneRotation(0));
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public JsonResult delHoneRotation(@PathVariable("id")String id) {
        return homeRotationService.deleteHoneRotation(id) ? JsonResult.ok() : JsonResult.error();

    }

}
